package gogogo.liwushuo.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import gogogo.liwushuo.R;
import gogogo.liwushuo.activity.SearchActivity;
import gogogo.liwushuo.activity.XuanLiShenQiActivity;

/**
 * Created by Godman_Kai on 2016/7/16.
 */
public class CategoryFragment extends Fragment {

    @InjectView(R.id.vp_category)
    ViewPager vpCategory;
    @InjectView(R.id.tab_category)
    TabLayout tabCategory;
    @InjectView(R.id.tv_cgtop_gift)
    TextView tvCgtopGift;
    private List<Fragment> mFragments;
    private String[] mTitles = {"攻略", "单品"};

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_category, container, false);
        ButterKnife.inject(this, view);
        mFragments = new ArrayList<>();
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mFragments.add(new CgLeftFragment());
        mFragments.add(new SingleProductFragment());

        vpCategory.setAdapter(new CategoryAdapter(getChildFragmentManager()));

        vpCategory.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                if (position == 0) {
                    tvCgtopGift.setVisibility(View.GONE);
                } else {
                    tvCgtopGift.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        tabCategory.setupWithViewPager(vpCategory);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    @OnClick({R.id.tv_cgtop_gift, R.id.fl_category_search})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_cgtop_gift:
                startActivity(new Intent(getActivity(), XuanLiShenQiActivity.class));
                break;
            case R.id.fl_category_search:
                startActivity(new Intent(getActivity(), SearchActivity.class));
                break;
        }
    }


    class CategoryAdapter extends FragmentPagerAdapter {
        public CategoryAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {

            return mTitles[position];
        }
    }
}
